<?php
namespace app\admin\controller;

use app\common\model\Article as ArticleModel;
use app\common\model\Category as CategoryModel;
use app\common\controller\AdminBase;
use app\common\model\User;
use think\Cache;
use think\Db;

/**
 * 文章管理
 * Class Article
 * @package app\admin\controller
 */
class Article extends AdminBase
{
    protected $article_model;
    protected $category_model;
    protected function _initialize()
    {
        parent::_initialize();
        $this->article_model  = new ArticleModel();
        $this->category_model = new CategoryModel();
        $category_level_list = $this->category_model->getLevelList();
        $this->assign('category_level_list', $category_level_list);
        //查询缓存权限值
        $identity_list_info = Cache::get('identity_list_info');
        $this->assign('identity_list_info',$identity_list_info);
    }

    /**
     * 文章管理
     * @param int    $cid     分类ID
     * @param int    $is_condition     是否加精/置顶
     * @param string $keyword 关键词
     * @param int    $page
     * @return mixed
     */
    public function index($cid = 0, $keyword = '', $page = 1)
    {
        $map   = [];
        $field = 'a_id, a_title, a_content, a_status,a_create_time, a_update_time';
//        if ($cid > 0) {
//            $category_children_ids = $this->category_model->where(['path' => ['like', "%,{$cid},%"]])->column('id');
//            $category_children_ids = (!empty($category_children_ids) && is_array($category_children_ids)) ? implode(',', $category_children_ids) . ',' . $cid : $cid;
//            $map['cid']            = ['IN', $category_children_ids];
//        }
        if (!empty($keyword)) {
            $map['title'] = ['like', "%{$keyword}%"];
        }
        $article_list  = $this->article_model->field($field)->where($map)->order(['a_id' => 'DESC'])->paginate(15, false, ['page' => $page]);
//        $category_list = $this->category_model->column('name', 'id');
        return $this->fetch('index', ['article_list' => $article_list, 'keyword' => $keyword]);
    }

    /**
     * 添加文章
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存文章
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'Article');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['a_create_time'] = time();
                $data['a_update_time'] = time();
                $aid = \app\common\model\Article::addArticle($data);
                if ($aid) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }
    /**
     * 禁用启用文章
     */
    public function editstatus($id, $status)
    {
        if ($status == 1) {
            $status = 2;
        } else if ($status == 2) {
            $status = 1;
        } else {
            $this->error('修改失败');
        }
        $aid = Db::name('article')->where('a_id', $id)->update(['a_status' => $status, 'a_update_time' => time()]);
        if ($aid) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    /**
     * 编辑文章
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $article = Db::name('article')->where('a_id', $id)->find();

        return $this->fetch('edit', ['article' => $article]);
    }

    /**
     * 更新文章
     * @param $id
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'Article');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['a_update_time'] = time();
                $aid = Db::name('article')->where('a_id', $data['a_id'])->update($data);
                if ($aid) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除文章
     * @param int   $id
     * @param array $ids
     */
    public function delete($id = 0, $ids = [])
    {
        $id = $ids ? $ids : $id;
        if ($id) {
            if ($this->article_model->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的文章');
        }
    }

    /**
     * 文章审核状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function toggle($ids = [], $type = '')
    {
        $data   = [];
        $status = $type == 'audit' ? 1 : 0;

        if (!empty($ids)) {
            foreach ($ids as $value) {
                $data[] = ['id' => $value, 'status' => $status];
            }
            if ($this->article_model->saveAll($data)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $this->error('请选择需要操作的文章');
        }
    }

    /**
     * 加精置顶推荐下载状态开关
     */
    public function topToggle($id){
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            if(@$data['api'] == 1){ //判断前台请求
                $field          =   $data['field'];
                $data[$field]   =   $data['rank'];
                unset($data['field']);
                unset($data['rank']);
                unset($data['api']);
            }
            if ($this->article_model->where(['id' => $id])->update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }
}